<template>
  <el-row
    type="flex"
    justify="space-between"
    align="middle"
    style="height: 40px; width: 100%;"
  >
    <el-col :span="20">
      <span>{{ nodeData.name }}</span>
    </el-col>
    <el-col :span="4">
      <el-row type="flex" justify="end">
        <!-- 两个内容 -->
        <el-col>{{ nodeData.manager }}</el-col>
        <el-col>
          <!-- 下拉菜单 element -->
          <!-- 下拉菜单 element -->
          <el-dropdown>
            <span @click.stop>操作<i class="el-icon-arrow-down" /> </span>
            <!-- 下拉菜单 -->
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native="isShow=true">添加子部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" @click.native="edit">编辑部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" @click.native="del">删除部门</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
    <!-- 新增部门的弹层 -->
    <el-dialog :title="showTitle" :visible="isShow" @close="close">
      <!-- 表单组件  el-form   label-width设置label的宽度   -->
      <!-- 匿名插槽 -->
      <el-form ref="deptForm" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="部门名称" prop="name">
          <el-input v-model="form.name" style="width:80%" placeholder="1-50个字符" />
        </el-form-item>
        <el-form-item label="部门编码" prop="code">
          <el-input v-model="form.code" style="width:80%" placeholder="1-50个字符" />
        </el-form-item>
        <el-form-item label="部门负责人" prop="manager">
          <el-select v-model="form.manager" style="width:80%" placeholder="请选择">
            <el-option v-for="item in people" :key="item.id" :label="item.username" :value="item.username" />
          </el-select>

        </el-form-item>
        <el-form-item label="部门介绍" prop="introduce">
          <el-input v-model="form.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" size="small" @click.native="add">确定</el-button>
          <el-button size="small" @click="close()">取消</el-button>
        </el-form-item>
      </el-form>

    </el-dialog>
  </el-row>
</template>

<script>
import { reqDelBumen, reqGetSimple, reqAddBumen, reqGetBumen, reqUpdateBumen } from '@/api/company'
export default {
  name: 'Tree',
  props: {
    // 定义传入的属性
    nodeData: {
      type: Object,
      required: true
    },
    isRoot: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      isShow: false,
      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: ['blur', 'change'] }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: ['blur', 'change'] }
        ],
        manager: [
          { required: true, message: '部门负责人不能为空', trigger: ['blur', 'change'] }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 300, message: '部门介绍要求1-300个字符', trigger: ['blur', 'change'] }
        ]
      },
      people: []
    }
  },
  computed: {
    showTitle() {
      return this.form.id ? '编辑部门' : '添加部门'
    }
  },
  created() {
    // console.log(1)
    this.getSimple()
  },
  methods: {
    async del() {
      const res = await this.$confirm('确定要删除该部门吗', '提示').catch(error => error)
      if (res !== 'confirm') return
      await reqDelBumen(this.nodeData.id)
      this.$message.success('删除成功')
      this.$emit('depts')
    },
    async getSimple() {
      const res = await reqGetSimple()
      this.people = res.data
      // console.log(this.people)
    },
    add() {
      this.$refs.deptForm.validate(async valid => {
        if (!valid) return
        if (this.form.id) {
          const res = await reqUpdateBumen(this.form)
          console.log(res)
        } else {
          await reqAddBumen({ ...this.form, pid: this.nodeData.id })
        }
        this.$message.success('操作成功')
        this.$emit('depts')
        this.close()
      })
    },
    close() {
      this.isShow = false
      this.form.name = ''
      this.form.code = ''
      this.form.manager = ''
      this.form.introduce = ''
      this.$refs.deptForm.resetFields()
    },
    async edit() {
      this.isShow = true
      const res = await reqGetBumen(this.nodeData.id)
      this.form = res.data
    }
  }
}
</script>
